<template>
  <el-card style="margin: 20px">
    <div class="topBox">
      <div class="topInput">
        <el-form style="width: 100%" ref="formRef" :model="searchData">
          <el-row>
            <el-col :span="6">
              <el-form-item label="标签名称" class="iscenter" prop="tags">
                <el-input
                  placeholder="请输入"
                  size="small"
                  v-model="searchData.tags"
                  style="width: 300px"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="城市" class="iscenter" prop="province">
                <el-select
                  placeholder="请选择"
                  size="small"
                  v-model="searchData.province"
                  @change="provincesChange"
                  style="width: 300px"
                >
                  <el-option
                    v-for="item in provincesList"
                    :key="item"
                    :label="item"
                    :value="item"
                    style="width: 300px"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="地区" class="iscenter" prop="city">
                <el-select
                  placeholder="请选择"
                  size="small"
                  v-model="searchData.city"
                  @change="citysChange"
                  style="width: 300px"
                >
                  <el-option
                    v-for="item in citysList"
                    :key="item"
                    :label="item"
                    :value="item"
                    style="width: 300px"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="企业名称" class="iscenter" prop="shortName">
                <el-input
                  size="small"
                  v-model="searchData.shortName"
                  style="width: 300px"
                ></el-input> </el-form-item
            ></el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="状态" class="iscenter" prop="state">
                <el-select
                  placeholder="请选择"
                  size="small"
                  v-model="searchData.state"
                  @change="stateChange"
                  style="width: 300px"
                >
                  <el-option
                    v-for="item in stateList"
                    :key="item.label"
                    :label="item.label"
                    :value="item.value"
                    style="width: 300px"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item style="padding-left: 13px">
                <el-button size="small" @click="clean">清除</el-button>
                <el-button type="primary" size="small" @click="search"
                  >搜索</el-button
                >
              </el-form-item></el-col
            >
            <el-col :span="12">
              <el-form-item style="text-align: right" class="rightBut">
                <el-button
                  type="success"
                  icon="el-icon-edit"
                  size="small"
                  @click="adduser"
                  >新增用户</el-button
                >
              </el-form-item></el-col
            >
          </el-row>
        </el-form>
      </div>
    </div>
    <el-alert
      style="margin-top: 20px"
      :title="`共${counts}条记录`"
      type="info"
      :closable="false"
      show-icon
    >
    </el-alert>
    <!-- 表格 -->
    <el-table style="margin-top: 20px" :data="userlist">
      <el-table-column type="index" label="序号" width="50"> </el-table-column>
      <el-table-column prop="number" label="企业编号" width="180">
      </el-table-column>
      <el-table-column prop="company" label="企业简称" width="80">
      </el-table-column>
      <el-table-column prop="tags" label="标签" width="80"> </el-table-column>
      <el-table-column prop="username" label="创建者" width="180">
      </el-table-column>
      <el-table-column prop="addDate" label="创建日期" width="180">
      </el-table-column>
      <el-table-column prop="remarks" label="备注" width="180">
      </el-table-column>
      <el-table-column prop="state" label="状态" width="80"> </el-table-column>
      <el-table-column prop="date" label="操作" width="180">
        <template #default="{ row }">
          <el-button
            icon="el-icon-edit"
            circle
            type="primary"
            plain
            @click="changeuser(row)"
          ></el-button>
          <el-button
            icon="el-icon-check"
            circle
            type="success"
            plain
            @click="changeuser(row)"
          ></el-button>
          <el-button
            icon="el-icon-delete"
            circle
            type="danger"
            plain
            @click="removeUser(row, row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block" style="margin: 25px">
      <el-pagination
        background
        @size-change="params.pagesize"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage3"
        :page-sizes="[10, 20, 30, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="counts"
      >
      </el-pagination>
    </div>
    <!-- 弹框 -->
    <el-dialog
      @close="handleClose"
      :title="title"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
    >
      <!-- 新增 -->
      <div v-if="addView">
        <el-form v-model="form" label-width="120px" ref="formRef1">
          <el-form-item label="企业名称">
            <el-input v-model="form.shortName"></el-input>
          </el-form-item>
          <el-form-item label="所属公司">
            <el-input v-model="form.company"></el-input>
          </el-form-item>
          <el-form-item label="城市地区">
            <el-select
              v-model="form.province"
              @change="provincesChange"
              placeholder="请选择"
            >
              <el-option
                v-for="item in provincesList"
                :key="item"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
            <el-select v-model="form.city" placeholder="请选择">
              <el-option
                v-for="item in citysList"
                :key="item"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="方向(企业标签)">
            <el-input v-model="form.tags"></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input
              type="textarea"
              v-model="form.remarks"
              placeholder="Please input"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>

      <!-- 修改 -->
      <div v-if="changeView">
        <el-form v-model="form" label-width="120px" ref="formRef1">
          <el-form-item label="企业名称">
            <el-input v-model="form.shortName"></el-input>
          </el-form-item>
          <el-form-item label="所属公司">
            <el-input v-model="form.company"></el-input>
          </el-form-item>
          <el-form-item label="城市地区">
            <el-select
              v-model="form.province"
              @change="provincesChange"
              placeholder="请选择"
            >
              <el-option
                v-for="item in provincesList"
                :key="item"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
            <el-select v-model="form.city" placeholder="请选择">
              <el-option
                v-for="item in citysList"
                :key="item"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="方向(企业标签)">
            <el-input v-model="form.tags"></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input
              type="textarea"
              v-model="form.remarks"
              placeholder="Please input"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import { list, add, remove, update } from "@/api/hmmm/companys";
import { provinces, citys } from "@/api/hmmm/citys";
export default {
  data() {
    return {
      searchData: {
        page: 1,
        pagesize: 10,
        tags: "",
        province: "",
        city: "",
        shortName: "",
        state: "",
      },
      provincesList: [], //城市
      citysList: [], //城市下市区
      stateList: [
        { label: "启用", value: 1 },
        { label: "禁用", value: 0 },
      ],
      id: "",
      changeView: false,
      title: "",
      addView: false,
      options: "",
      keyword: "",
      form: {
        company: "",
        tags: "",
        province: "",
        city: "",
        shortName: "",
        state: "",
        isFamous: 1,
      },
      dialogVisible: false,
      currentPage3: null,
      username: "",
      pages: "",
      input: "",
      counts: null,
      userlist: [],
      params: {
        page: 1,
        pagesize: 10,
        username: "",
      },
    };
  },
  methods: {
    //列表
    async list() {
      const { data } = await list(this.params);
      console.log(data);
      this.userlist = data.items;
      this.counts = data.counts;
      this.pages = data.pages;
      console.log(this.userlist);
      this.userlist.forEach((item) => {
        if (item.state === 0) {
          item.state = "禁用";
        } else {
          item.state = "启用";
        }
      });
    },
    //分页
    handleCurrentChange(current) {
      this.params.page = current;
      this.list();
    },
    //搜索
    async search() {
      const res = await list(this.searchData);
      this.counts = res.data.counts;
      this.userlist = res.data.items;
      console.log(res);
      // this.list();
    },
    //添加
    async adduser() {
      this.dialogVisible = true;
      this.addView = true;
      this.changeView = false;
      this.title = "添加用户";

      if (this.form.isFamous === 1) {
        this.form.isFamous = true;
      } else {
        this.form.isFamous = false;
      }
    },
    //修改
    async changeuser(row) {
      console.log(row);
      this.form = row;
      this.id = row.id;
      this.dialogVisible = true;
      this.addView = false;
      this.changeView = true;
      this.title = "编辑用户";
      if (this.form.state === "启用") {
        this.form.state = 1;
      } else {
        this.form.state = 0;
      }
      if (this.form.isFamous === 1) {
        this.form.isFamous = true;
      } else {
        this.form.isFamous = false;
      }
      // const { data } = await simple();
      // this.options = data;
    },
    //删除
    async removeUser(row, id) {
      console.log(id);
      const res = await remove(row, id);
      this.$message.success("删除用户成功");
      console.log(res);
      this.list();
    },
    //清空页面
    handleClose() {
      this.dialogVisible = false;
      this.$refs.formRef.resetFields();
      this.form = {};
    },
    //弹框确认
    async submit() {
      console.log(111);
      if (this.title === "添加用户") {
        await add(this.form);
        this.$message.success("新增用户成功");
      } else {
        await update(this.form, this.id);
        this.$message.success("编辑用户成功");
      }
      this.dialogVisible = false;
      this.list();
    },
    provincesChange(val) {
      this.citysList = citys(val);
      this.searchData.province = val;
    },
    citysChange(val) {
      this.searchData.city = val;
    },
    stateChange(val) {
      this.searchData.state = val;
    },

    clean() {
      this.$refs.formRef.resetFields();
    },
  },
  created() {
    this.list();
    this.provincesList = provinces();
  },
};
</script>

<style scoped lang="scss"></style>
